# -*- coding: utf-8 -*-
import sys,androidTools,Const,Util,json,MerchantConst
from channel import getModelInfo

def getAChannelApk(name_apk_base,channel_id ,path_apk_channel,FROM,pppp,xxx,yyy):
    '''
    修改包渠道号
    '''
    #对渠道号文件进行修改 Const.PATH_APK_FILE_SOURCEID为apk解压后一文件路径
    Util.changeChannel(Const.PATH_APK_FILE_SOURCEID, channel_id)
    print Const.PATH_APK_FILE_SOURCEID
    print channel_id
    #直接在APK文件中修改渠道号
    androidTools.zipAddFile(name_apk_base,Const.PATH_APK_FILE_SOURCEID)
    print name_apk_base
    print Const.PATH_APK_FILE_SOURCEID
    #对未签名的APK包，进行签名

    #androidTools.SignApk(Const.BASE_NAME_APKUNALIGN,name_apk_base,Const.PATH_KEYSTORE,Const.NAME_KEYSTORE,Const.SIGN_KEYPASSWD)
    #androidTools.SignApk(Const.BASE_NAME_APKUNALIGN,name_apk_base,PATH_KEYSTORE,NAME_KEYSTORE,SIGN_KEYPASSWD)
    androidTools.SignApk(Const.BASE_NAME_APKUNALIGN,name_apk_base,pppp,xxx,yyy)
    androidTools.zipalign(Const.BASE_NAME_APKUNALIGN,path_apk_channel)

if __name__ == '__main__':

    # argLen = len(sys.argv)
    # if(argLen < 5):
    #     print ('参数个数错误！')
    #     exit(1)
    # VERSION = sys.argv[1]
    # DIR_APK_BASE = sys.argv[2]
    # DIR_APK_CHANNEL = sys.argv[3]
    # IdList = sys.argv[4]
    # NameList = sys.argv[5]
    # FROM = sys.argv[6]
    # CurrentVersion = sys.argv[7]
    # NAME_KEYSTORE = sys.argv[8]
    # SIGN_KEYPASSWD = sys.argv[9]
    # idList = IdList.split(',')
    # nameList = NameList.split(',')
    # PATH_KEYSTORE = FROM+'/tools/'+FROM+'.keystore'
    #
    #
    # for channelName in packChannels:
    #    print channelName + " * "
    # CHANNELS = '[{"code":"5002a","name":"zhenxiangtoutiao"},{"code":"5001a","name":"zhenxiangsouhu"},{"code":"4009a","name":"qqliulanqi"},{"code":"4008a","name":"360ce8"},{"code":"4007a","name":"360ce7"},{"code":"4006a","name":"360ce6"},{"code":"4005a","name":"360ce5"},{"code":"4004a","name":"360ce4"},{"code":"4003a","name":"360ce3"},{"code":"4002a","name":"360ce2"},{"code":"4001a","name":"360ce1"},{"code":"3038a","name":"360SEM"},{"code":"3037a","name":"VIVO"},{"code":"3036a","name":"coolpad"},{"code":"3035a","name":"leshi"},{"code":"huidu","name":"huidu"},{"code":"lg1","name":"lg1"},{"code":"lg2","name":"lg2"},{"code":"lg3","name":"lg3"},{"code":"lg4","name":"lg4"},{"code":"lg5","name":"lg5"},{"code":"lg6","name":"lg6"},{"code":"3033a","name":"tengxunguanjia"},{"code":"3032a","name":"yunyinghuodong"},{"code":"jfq","name":"jfq"},{"code":"3001a","name":"360"},{"code":"3002a","name":"baidu"},{"code":"3003a","name":"91"},{"code":"3004a","name":"hiapkmarket"},{"code":"3005a","name":"qq"},{"code":"3006a","name":"wdj"},{"code":"3007a","name":"goapk"},{"code":"3008a","name":"appchinamarket"},{"code":"3009a","name":"mumayi"},{"code":"3010a","name":"gfan"},{"code":"3011a","name":"netease"},{"code":"3012a","name":"eoemarket"},{"code":"3013a","name":"nduoa"},{"code":"3014a","name":"ppuc"},{"code":"3015a","name":"oppo"},{"code":"3016a","name":"huaweidevice"},{"code":"3017a","name":"jinli"},{"code":"3018a","name":"sougou"},{"code":"3019a","name":"lenovo"},{"code":"3020a","name":"samsung"},{"code":"3021a","name":"xiaomi"},{"code":"3022a","name":"meizu"},{"code":"3023a","name":"10086mm"},{"code":"3024a","name":"wostore"},{"code":"3025a","name":"189storepublic"},{"code":"3026a","name":"google"},{"code":"appstore","name":"appstore"},{"code":"2001a","name":"guanwang"},{"code":"2002a","name":"weibo"},{"code":"10001a","name":"weiboqiangqian"},{"code":"10002a","name":"weixinqiangqian"},{"code":"10003a","name":"qiangqianyaoqing"},{"code":"jdb","name":"jdbshengji"},{"code":"3027a","name":"weiboyingyong"},{"code":"2003a","name":"weixin"},{"code":"10004a","name":"pengyouH5"},{"code":"10005a","name":"jiyinH5"},{"code":"10006a","name":"xinshouH5"},{"code":"10007a","name":"qiangqianditui1"},{"code":"10008a","name":"qiangqianditui2"},{"code":"10009a","name":"qiangqianditui3"},{"code":"10010a","name":"qiangqianditui4"},{"code":"10011a","name":"qiangqianditui5"},{"code":"10012a","name":"qiangqianditui6"},{"code":"10013a","name":"qiangqianditui7"},{"code":"10014a","name":"qiangqianditui8"},{"code":"10015a","name":"qiangqianditui9"},{"code":"10016a","name":"qiangqianditui10"},{"code":"3028a","name":"sinayingyong"},{"code":"3034a","name":"baiduSEM"},{"code":"3030a","name":"UCSEM"},{"code":"3031a","name":"sougouSEM"}]'
    # json_cannels = json.loads(CHANNELS)
    # cannel_list = json_cannels['channels']
    # name_apk_base = Const.NAME_FILE_PREFIX + VERSION + '.apk'



    '''******************************************************************************'''
    VERSION = '3.0.4.0'
    DIR_APK_BASE = './channel/baseapk'
    DIR_APK_CHANNEL = './channelApk/'+'jdb'+'-'+VERSION+'/'
    IdList = getModelInfo.get_360_info_channelNum()
    NameList = getModelInfo.get_360_info_channelName()
    FROM = 'jdb'
    CurrentVersion = '3040'
    NAME_KEYSTORE = getModelInfo.get_keyAlias(FROM, 'android')
    SIGN_KEYPASSWD = keyPassword = getModelInfo.get_keyPassword(FROM, 'android')
    idList = IdList.split(',')
    nameList = NameList.split(',')
    PATH_KEYSTORE = FROM + '/tools/' + FROM + '.keystore'


    name_apk_base = Const.NAME_FILE_PREFIX + VERSION + '.apk'
    print 'name apk base = '+name_apk_base;

    # 例：jdb/340_bd base/apk/jdb_289.apk 基础包路径
    path_apk_base = FROM + '/' + DIR_APK_BASE + '/' + name_apk_base
    print 'path_apk_base = ' + path_apk_base;

    # 例: jdb/channel/3040
    dir_apk_channel = FROM + '/channel/'+CurrentVersion+'/'
    print 'dir_apk_channel = ' + dir_apk_channel;





    #创建该版本对应的渠道包文件夹（版本名去新建文件夹名）
    Util.makedirs(dir_apk_channel)

    #先将母包拷贝出来
    print ("Util.copyFile path:")
    print path_apk_base
    Util.copyFile(path_apk_base,'./')


    #生成每个渠道包
    for index in range(len(idList)):
        channel_code = idList[index]
        channel_name = nameList[index]
        #print channelString;
        #nPos = channelString.find(channel_name)
        #print nPos;
        path_apk_channel = dir_apk_channel+FROM+'_'+channel_name+'_'+VERSION+'_'+channel_code+'.apk'
        print "path apk channel = " + path_apk_channel
        getAChannelApk(name_apk_base,channel_code,path_apk_channel,FROM)

    #清理操作
    #Util.DelFolder(Const.BASE_NAME_APKDIR)
    Util.DelFile(name_apk_base)
    Util.DelFile(Const.BASE_NAME_APKUNALIGN)
